package com.cn.spring.web.utils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.ResourceBundle;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
/**
 * @author keep_trying
 *
 */
public class YLJedisUtil {
	/**
	 * Logger for this class
	 */
	//private final static Logger logger = LogFactory.getInstance().getInfo();
	
	private static JedisPool pool;

	static {
		pool = getJedisPool();
	}

	private static JedisPool initJedisPool() {
		//读出配置文件
		ResourceBundle rb = ResourceBundle.getBundle("config");
		return new JedisPool(new JedisPoolConfig(), 
				rb.getString("redis.host"), 
				"".equals(rb.getString("redis.port"))?6379:Integer.valueOf(rb.getString("redis.port")), 
				"".equals(rb.getString("redis.timeout"))?0:Integer.valueOf(rb.getString("redis.timeout")),
				"".equals(rb.getString("redis.password"))?null:rb.getString("redis.password"),
				"".equals(rb.getString("redis.database"))?0:Integer.valueOf(rb.getString("redis.database")));
	}

	/**
	 *
	 * @Description:获取JedisPool
	 * @Author:T.L
	 * @Date:2015-7-30 下午8:41:57
	 * @return
	 *
	 */
	private static JedisPool getJedisPool() {
		if (null == pool || pool.isClosed()) {
			return pool = initJedisPool();
		}
		return pool;
	}
	
	
	/**
	 * 
	 *
	 * @Description:按步长增加
	 * @Author:T.L
	 * @Date:2015-7-30 下午8:42:45
	 * @param key 键
	 * @param value 步长值
	 * @return
	 *
	 */
	public static long incrBy(String key, long value) {
		Jedis jedis = getJedisPool().getResource();
		long i = jedis.incrBy(key, value);
		jedis.close();
		return i;
	}

	/**
	 * 
	 *
	 * @Description:按步长减少
	 * @Author:T.L
	 * @Date:2015-7-30 下午8:43:07
	 * @param key 键
	 * @param value 步长值
	 * @return
	 *
	 */
	public static long decrBy(String key, long value) {
		Jedis jedis = getJedisPool().getResource();
		long i = jedis.decrBy(key, value);
		jedis.close();
		return i;
	}

	/**
	 * 
	 *
	 * @Description:根据KEY查询
	 * @Author:T.L
	 * @Date:2015-7-30 下午8:43:29
	 * @param key 键
	 * @return
	 *
	 */
	public static String getKey(String key) {
		Jedis jedis = getJedisPool().getResource();
		String s = jedis.get(key);
		jedis.close();
		return s;

	}
	/**
	 * 
	 *
	 * @Description:增加键值存储
	 * @Author:T.L
	 * @Date:2015-7-31 上午1:23:14
	 * @param key 键
	 * @param value 值
	 * @return
	 *
	 */
	public static String setKey(String key, String value){
		Jedis jedis = getJedisPool().getResource();
		String s = jedis.set(key, value);
		jedis.close();
		return s;
	}
	
	/**
	 * 
	 *
	 * @Description:增加键值存储,并设置过期时间,秒为单
	 * @Author:T.L
	 * @Date:2015-8-4 下午1:44:13
	 * @param key 键
	 * @param value 值
	 * @param time 限制时间,单位:秒
	 * @return
	 *
	 */
	public static String setKye(String key, String value,int time){
		Jedis jedis = getJedisPool().getResource();
		String s = jedis.setex(key, time, value);
		jedis.close();
		return s;
	}

	public static void main(String[] args) {
		YLJedisUtil.setKey("miOrders_123456", "1");
		YLJedisUtil.setKey("miOrders_222", "0");

	}
	public static String saveInfo(String key, String value){
		String realkey = setKey(key, value);
		return realkey;
	}
	public static String getInfo(String key){
		String realkey = getKey(key);
		return realkey;
	}
	public static long delInfo(String key){
		Jedis jedis = getJedisPool().getResource();
		long time = jedis.del(key);
		jedis.close();
		return time;
	}
	
	/**
	 * 
	 * @Title: getSystime   
	 * @Description: 取redis系统时间  
	 * @param: @return      
	 * @return: String
	 * @author liuwei      
	 * @throws
	 */
	public static String getSystime(){
		Jedis jedis = getJedisPool().getResource();
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		List<String> times = new ArrayList<String>();
		String sysdate = sdf.format(new Date());;
		try{
			times = jedis.time();
			sysdate = sdf.format(new Date(Long.parseLong(times.get(0))*1000));
			jedis.close();
		}catch (Exception e) {
			sysdate = sdf.format(new Date());
			return sysdate;
		}
		return sysdate;
	}
	
}